package ynu.elm.Dao;
import tools.utils;
import ynu.elm.entities.User;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Client2UserDao extends User {
    public Client2UserDao(ClientUserDao user) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            MenuDao menu = new MenuDao();
            menu.showMenu("商家二级");
            System.out.print("请输入选择：");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    showAllFood(user);
                    break;
                case 2:
                    addFood(user);
                    break;
                case 3:
                    updateFood(user);
                    break;
                case 4:
                    delFood(user);
                    break;
                case 5:
                    findfood();
                case 6:
                    return;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }
    private void showAllFood(ClientUserDao user) {
        try {
            user.getResource().setCon(utils.getConnection());
            user.getResource().setSt(user.getResource().getCon().createStatement());
            String sql="SELECT * FROM `food`";
            user.getResource().setRs(user.getResource().getSt().executeQuery(sql));
            user.getFormatter().format("%-5s \t%-10s \t%-10s \t%-5s","食品编号","食品名称","食品介绍","食品价格");
            System.out.println();
            while(user.getResource().getRs().next()){
                user.getFormatter().format("%-5d \t%-10s \t%-10s \t%-5f",user.getResource().getRs().getObject("id"),user.getResource().getRs().getObject("name"),user.getResource().getRs().getObject("introduce"),user.getResource().getRs().getObject("money"));
                System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(user.getResource().getRs(),user.getResource().getSt(),user.getResource().getCon());
        }
    }
    private void updateFood(ClientUserDao user) {
        int cnt=0;
        int nName=0,nIntro=0,nMoney=0,nId=0;
        String name=null,introduce=null;
        int id=0;
        float money=0;
        Scanner sc = new Scanner(System.in);
        String flag="n";
        String sql="UPDATE `food` SET ";
        //显示
        System.out.println("请输入更新的编号：");
        id=sc.nextInt();
        showSigleFood(user,id);
        System.out.println("是否更新食品名称(y/n)：");
        flag=sc.next();
        if(flag.equals("y")){
            sql=sql+"`name`=?,";
            System.out.println("请输入新食品名称：");
            name=sc.next();
            nName=++cnt;
        }
        System.out.println("是否更新食品介绍(y/n)：");
        flag=sc.next();
        if(flag.equals("y")){
            sql=sql+"`introduce`=?,";
            System.out.println("请输入新食品介绍：");
            introduce=sc.next();
            nIntro=++cnt;
        }
        System.out.println("是否更新食品价格(y/n)：");
        flag=sc.next();
        if(flag.equals("y")){
            sql=sql+"`money`=?,";
            System.out.println("请输入新食品价格：");
            money=sc.nextFloat();
            nMoney=++cnt;
        }
        if(cnt==0) return;
        sql=sql+"WHERE `number`=?";
        nId=++cnt;
        if(cnt>0){
            try {
                user.getResource().setCon(utils.getConnection());
                PreparedStatement preparedStatement = user.getResource().getCon().prepareStatement(sql);
                preparedStatement.setString(nName,name);
                preparedStatement.setString(nIntro,introduce);
                preparedStatement.setFloat(nMoney,money);
                preparedStatement.setObject(nId,id);
                boolean execute = preparedStatement.execute();
                if (!execute) System.out.println("修改食品成功！");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                utils.release(user.getResource().getRs(),user.getResource().getSt(),user.getResource().getCon());
            }
        }

    }
    private void addFood(ClientUserDao user) {
        String name=null,introduce=null;
        float money=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入食品名称：");
        name=sc.next();
        System.out.println("请输入食品介绍：");
        introduce=sc.next();
        System.out.println("请输入食品价格：");
        money=sc.nextFloat();
        try {
            user.getResource().setCon(utils.getConnection());
            String sql="INSERT INTO `food`(`name`,`introduce`,`money`) VALUES (?,?,?)";
            PreparedStatement preparedStatement = user.getResource().getCon().prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,introduce);
            preparedStatement.setFloat(3,money);
            boolean execute = preparedStatement.execute();
            if (!execute) System.out.println("新增食品成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(user.getResource().getRs(),user.getResource().getSt(),user.getResource().getCon());
        }
    }
    private void delFood(ClientUserDao user) {
        Scanner sc = new Scanner(System.in);
        showAllFood(user);
        System.out.println("请选择要删除的食品的编号：");
        int id=sc.nextInt();
        System.out.println("确认要删除吗(y/n)");
        String select=sc.next();
        if (select.equals("y")){
            try {
                user.getResource().setCon(utils.getConnection());
                String sql="DELETE FROM `food` WHERE `id`=?";
                PreparedStatement preparedStatement = user.getResource().getCon().prepareStatement(sql);
                preparedStatement.setInt(1,id);
                preparedStatement.execute();
                System.out.println("删除食品成功！");
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                utils.release(user.getResource().getRs(),user.getResource().getSt(),user.getResource().getCon());
            }

        }
    }
    private void showSigleFood(User user,int id){
        try {
            user.getResource().setCon(utils.getConnection());
            String sql="SELECT * FROM `food` WHERE `id`=?";
            PreparedStatement preparedStatement = user.getResource().getCon().prepareStatement(sql);
            preparedStatement.setInt(1,id);
            user.getResource().setRs(preparedStatement.executeQuery());
            while(user.getResource().getRs().next()){
                System.out.println("食品编号："+user.getResource().getRs().getObject("id"));
                System.out.println("食品名称："+user.getResource().getRs().getObject("name"));
                System.out.println("食品介绍："+user.getResource().getRs().getObject("introduce"));
                System.out.println("食品价格："+user.getResource().getRs().getObject("money"));
                System.out.println("所属商家：10001");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(user.getResource().getRs(), user.getResource().getSt(), user.getResource().getCon());
        }
    }
    private void findfood(){
        String sql="SELECT * FROM `food`";
        //connection
        try {
            int nameflag=0,introflag=0;
            Scanner sc=new Scanner(System.in);
            String fName = null,fIntro=null;
            System.out.println("是否需要输入菜品名称(y/n)：");
            if(sc.next().equals("y")){
                System.out.println("请输入菜品名称关键词：");
                fName=new Scanner(System.in).next();
                sql=sql+"WHERE `name` LIKE ? ";
                nameflag=1;
            }
            System.out.println("是否需要输入菜品介绍(y/n)：");
            if(sc.next().equals("y")){
                System.out.println("请输入菜品介绍关键词：");
                fIntro=new Scanner(System.in).next();
                if(nameflag==1) sql=sql+"AND ";
                else sql=sql+"where ";
                sql=sql+"`introduce` LIKE ?";
                introflag=1;
            }
            this.getResource().setCon(utils.getConnection());
            //preparedStatement
            PreparedStatement preparedStatement = this.getResource().getCon().prepareStatement(sql);
            //resultSet
            if(nameflag==1) preparedStatement.setString(1,"%"+fName+"%");
            if(introflag==1 && nameflag==1) preparedStatement.setString(2,"%"+fIntro+"%");
            if(introflag==1 && nameflag==0) preparedStatement.setString(1,"%"+fIntro+"%");
            this.getResource().setRs(preparedStatement.executeQuery());
            Printf(this.getResource().getRs());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private void Printf(ResultSet rs) throws SQLException {
        this.getFormatter().format("%-10s\t%-25s   \t%-25s  \t%-10s ","菜品编号","菜品名称","菜品介绍","菜品价格");
        System.out.println();
        while (rs.next()){
            this.getFormatter().format("%-10s\t%-25s   \t%-25s   \t%-10s",rs.getObject("id"),rs.getObject("name"),rs.getObject("introduce"),rs.getObject("money"));
            System.out.println();
        }
    }
}
